# -*- coding: utf-8 -*-
"""
Created on Thu Oct 21 21:09:55 2021

@author: Administrator
"""

    



#缩略图
def slv(copy):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy()
    w,h = im_copy.size#分别得到图像的宽、高
    im_copy.thumbnail((w//10,h//10))
    im_copy.show()

#保存缩略图
    im_copy.save('sl_a.jpg')

#等比例缩略
def sl1(suo):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy()
    w,h = im_copy.size#分别得到图像的宽、高        
    im_size = im_copy.resize((w//4,h//4))
    im_size.show()
    
# 自由裁剪大小函数
def caijian(image):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy()        
    box = (800,200,1300,800)  # 截图的开始、结束坐标
    region = im_copy.crop(box)
    region.show()
    region.save('cj_a.jpg')

#水平旋转
def xz(im):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy() 
    im_copy.rotate(180).save('sz_a.jpg')


#镜像翻转
def jxzy(img):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy() 
    im_copy.transpose(Image.FLIP_LEFT_RIGHT).save('jxzy_a.jpg')
def jxsx(image):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy() 
    im_copy.transpose(Image.FLIP_TOP_BOTTOM).save('jxsx_a.jpg')
#叠加---添加logo、水印
def djia(image,image_log):
    from PIL import Image
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy=im.copy()
    im_log = Image.open('STORE.JPG')
    lw,lh = im_log.size

    im_log_copy = im_log.copy()
    im_log_copy.thumbnail((lw//10,lh//10))

    im_copy.paste(im_log_copy)
    im_copy.show()
    im_copy.save('tjsy_a.jpg')

#高级
# 1、增强颜色
def zq(im): 
    from PIL import Image       
    from PIL import ImageEnhance as ie
    im = Image.open('GIRL.JPG')
    im.show()
    im_copy = im.copy() 
    enh = ie.Contrast(im_copy)
    enh.enhance(1.5).show()  # 增强 50% 对比度

#2平滑
def ph(img):
    import numpy as np
    from scipy import signal
    from skimage import data
    from matplotlib import pyplot as plt
    def corre12d(img,window):
        s=signal.correlate2d(img,window,mode='same',boundary='fill')
        return s.astype(np.uint8)
        img=data.camera()
        window1=np.ones((3,3))/(3**3)
        window2=np.ones((5,5))/(5**2)
        window3=np.ones((9,9))/(9**2)
        new_img1=corre12d(img,window1)
        new_img2=corre12d(img,window2)
        new_img3=corre12d(img,window3)
        plt.figure()
        plt.imshow(img,cmap='gray')
        plt.figure()
        plt.imshow(new_img1,cmap='gray')
        plt.figure()
        plt.imshow(new_img2,cmap='gray')
        plt.figure()
        plt.imshow(new_img3,cmap='gray') 



#4、颜色分层
def RGBfc(img):
    from skimage import data,color
    from matplotlib import pyplot as plt
    import numpy as np
    img=data.coffee()
#将彩色图像转换为灰度图像
    grayimg=color.rgb2gray(img)
    plt.figure()
    plt.axis('off')
#显示灰度图像
    plt.imshow(grayimg,cmap='gray')
    rows,cols=grayimg.shape
    labels=np.zeros([rows,cols])
    for i in range(rows):
        for j in range(cols):
            
            if(grayimg[i,j]<0.4):
                
                labels[i,j]=0
                
            elif(grayimg[i,j]<0.8):
                labels[i,j]=1
            else:
                labels[i,j]=2
                psdimg=color.label2rgb(labels)#不同的灰度区间采用不同的颜
                plt.figure()
                plt.axis('off')
                plt.imshow(psdimg)

# 5、 调整gamma值  --25页
def gamma(image):
    from skimage import data,io,exposure
    from matplotlib import pyplot as plt
    image=data.coffee()#读入图像
#分别计算gamma=0.1，0.5，0.9时的图像
    image_1=exposure.adjust_gamma(image,0.1)
    image_2=exposure.adjust_gamma(image,0.5)
    image_3=exposure.adjust_gamma(image,0.9)
#分别展示原图及结果图像
    plt.subplot(2,2,1)
    plt.title('gamma=1')
    io.imshow(image)
    plt.subplot(2,2,2)
    plt.title('gamma=0.1')
    io.imshow(image_1)
    plt.subplot(2,2,3)
    plt.title('gamma=0.5')
    io.imshow(image_2)
    plt.subplot(2,2,4)
    plt.title('gamma=0.9')
    io.imshow(image_3)
    plt.show()

#6.傅里叶变换    
def fu(image):
    from skimage import data,color
    from matplotlib import pyplot as plt
    import numpy as np
    img_fu=data.coffee()
    img1_fu=img_fu
    img_fu=color.rgb2gray(img_fu)
    m,n=img_fu.shape
    fx=img_fu
    for x in range(n):
        fx[:,x]=np.fft.fft(img_fu[:,x])
        for y in range(m):
            fx[y,:]=np.fft.fft(img_fu[y,:])
            fshift=np.fft.fftshift(fx)
            fimg=np.log(np.abs(fshift))
            plt.subplot(121)
            plt.imshow(img1_fu,'gray')
            plt.title('原始图像')
            plt.subplot(122)
            plt.imshow(fimg,'gray')
            plt.title('两次一维傅里叶变换后的图像')
            plt.show()

import tkinter
top = tkinter.Tk()
top.title('图像处理')
top.geometry('300x300+600+300')
top.configure(background='Pink')
label=tkinter.Label(top, text="图像小工具",bg="Pink",fg = "Gray").place(x=80,y=10)
#tkinter.Label(top, text="当前路径:",bg="Pink").place(x=15,y=50)

a=tkinter.Button(top, text="缩略:",bg="Pink",command=slv).place(width=20,height=40,x=50,y=100)
#a.grid(column = 1, row = 1)
#a.pack(side='right')
b=tkinter.Button(top, text="裁剪",bg="Pink",command=caijian).place(width=30,height=50,x=50,y=100)
c=tkinter.Button(top, text="旋转",bg="Pink",command=xz).place(width=40,height=60,x=50,y=100)
#tkinter.Button(top, text="图像整理",bg="Pink",command=lambda.option).place(width=200,height=50,x=50,y=100) 
#tkinter.Button(top, text="退出",command=lambda.option).place(width=3,height=2)
# 进入消息循环
top.mainloop()




    


